<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY
// |-----------------------------------------------------------------------------------
// | Author: 贝贝 <hebiduhebi@163.com>
// | Copyright (c) 2013-2015, http://www.gooraye.net. All Rights Reserved.
// |-----------------------------------------------------------------------------------

namespace Tool\Api;
use Common\Api\Api;
use Tool\Model\ProvinceModel;


class ProvinceApi extends Api{
    /**
     * 查询，不分页
     */
    const QUERY_NO_PAGING = "Tool/Province/queryNoPaging";
    /**
     * 添加
     */
    const ADD = "Tool/Province/add";
    /**
     * 保存
     */
    const SAVE = "Tool/Province/save";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "Tool/Province/saveByID";
    /**
     * add by zhouzhou
     */
    const DELETE_BY_ID = "Tool/Province/deleteByID";

    /**
     * 删除
     */
    const DELETE = "Tool/Province/delete";

    /**
     * 查询
     */
    const QUERY = "Tool/Province/query";

    /**
     * 查询一条数据
     */
    const GET_INFO = "Tool/Province/getInfo";

	protected function _init(){
		$this->model = new ProvinceModel();
	}
    public function deleteByID($id=0){
        $id = intval($id) | 0;
        $r2 = apiCall(ProvinceApi::GET_INFO,array(array('id'=>$id)));
        if(!$r2['status']) $this->apiReturnErr($r2['info']);
        if($r2['info']){
            $r  = apiCall(ProvinceApi::DELETE,array(array('id'=>$id)));
            if(!$r['status'])   $this->apiReturnErr($r['info']);
            $fid = $r2['info']['provinceid'];unset($r2);
            // echo '删除'.$fid;
            if(!$fid)    $this->apiReturnErr('null provinceid ');
$model = $this->model;
$model -> startTrans();
            //查询是否有下级市
            $r = apiCall(CityApi::QUERY_NO_PAGING,array(array('father'=>$fid)));
            if(!$r['status']){
$model ->rollback();$this->apiReturnErr($r['info']);
            }else{
                if($r['info']){
                    //有下级市
                    foreach ($r['info'] as $k => $v) {
                        // dump($v);exit;
                        $fid = $v['cityid'];
                        if(!$fid){
$model ->rollback();$this->apiReturnErr('cityid is null');
                        }
                        $r  = apiCall(CityApi::DELETE,array(array('cityID'=>$fid)));
                        if(!$r['status']){
$model ->rollback();$this->apiReturnErr($r['info']);
                        }
                        // echo '删除'.$fid;
                        //删除下级区
                        $r  = apiCall(AreaApi::DELETE,array(array('father'=>$fid)));
                        if(!$r['status']){
$model ->rollback();$this->apiReturnErr($r['info']);
                        }
                        // echo '删除area.father='.$fid;
                    }
                }
                $this->apiReturnSuc('删除成功');
            }
        }else{
            $this->apiReturnErr('null province');
        }
    }
}
